
<?php
include ('../funct.php');
 html();

 function getDataFromPesel ($pesel, $data_wizyty){
     
     $rok = '19'.substr($pesel, 0, 2);
     $mies = substr($pesel, 2, 2);
     if ($mies > 20) {
         $rok = $rok+100;
         $mies = sprintf("%02s",$mies -20);
     }
     $dzien = substr($pesel, 4, 2);
     $wiek = date("Y", (strtotime($data_wizyty))) - date("Y", strtotime($rok.'-'.$mies.'-'.$dzien));
     #echo $pesel.'-------'.$wiek.'----------'.$rok.'-'.$mies.'-'.$dzien.'<br>';
     #echo $pesel.'----'.substr($pesel, 9,1).'.....'.fmod(substr($pesel, 9,1), 2).'<br>';
     if (fmod(substr($pesel, 9,1), 2) == 0) $plec = 'K';
     else $plec='M';
             
     $gus_info['wiek_pac']=$wiek;
     $gus_info['plec_pac']=$plec;
     return $gus_info;
     
 }
 
echo '<table border="1" cellpadding="5" align="center">';
 
 $sql="select sl_gab.nazwa, count(wizyty.id), sl_gab.id from wizyty, sl_gab where wizyty.akt = 1 and wizyty.id_gab = sl_gab.id  and year(wizyty.data) = '2014' group by sl_gab.nazwa";
 #echo $sql;

 #echo '<table class="linia" align="center">';
$result=mysql_query($sql);
	while($row = mysql_fetch_array($result))
	{
	
	echo '<tr><td class="linia" align="center" colspan=5>';
	echo '<b>'.$row[0].' - '.$row[1].' pacjentów</b><br>';
	
        $razem=0;
	$do18=0;
	$pow65=0;
	$kob=0;
        
//	$sql2="select sl_uslugi.id, 
//	(case 
//	when (timestampdiff(YEAR,pac.pac_dataur,wizyty.data) < 18) then '18' 
//	
//	 end) AS wiek,
//         (case 
//	
//	when (timestampdiff(YEAR,pac.pac_dataur,wizyty.data) > 65) then '65' 
//	 end) AS wiek2,
//	
//	count(sl_uslugi.id)
//	from pac, wizyty, sl_uslugi 
//	where
//	sl_uslugi.id = wizyty.typ_wiz and pac.id = wizyty.id_pac and year(wizyty.data) = '2014' and wizyty.akt = 1 and wizyty.id_gab = ".$row[2]." 
//	and (
//(timestampdiff(YEAR,pac.pac_dataur,wizyty.data) < 18)
//or 
//(timestampdiff(YEAR,pac.pac_dataur,wizyty.data) > 65)
//)
//        group by sl_uslugi.nazwa, wiek, wiek2
//	";

        $sql2="select pac.pac_pesel, date(wizyty.data), wizyty.typ_wiz
from wizyty left join pac on pac.id = wizyty.id_pac
where id_gab = ".$row[2]."
and year(wizyty.data) = '2014'
and wizyty.akt =1
and pac.pac_pesel<>''";
        
	#echo $sql2;
	
	
	$result2=mysql_query($sql2);
        $ile18=0;
	while($row2 = mysql_fetch_array($result2))
	{
          $x= getDataFromPesel($row2[0], $row2[1]);  
          
	$id_u=$row2[2];
	
        if ($x['wiek_pac']<18) {
            $pods[$row2[2]]['do18']=$pods[$row2[2]]['do18']+1;
            
        } 
        elseif ($x['wiek_pac']>'65') $pods[$row2[2]]['pow65']=$pods[$row2[2]]['pow65']+1;
        if ($x['plec_pac']=='K') $pods[$row2[2]]['kob']=$pods[$row2[2]]['kob']+1;
	#echo '<tr><td class="linia">'.$row2[0].'</td><td class="linia">'.$row2[1].'</td><td class="linia">'.$row2[2].'</td><td class="linia">'.$row2[3].'</td></tr>';
	}
        
        
        
	#echo '<pre>';
        #print_r($pods);
	#unset($do18, $pow65, $kob);
	
	echo '<tr><td class="linia">Usługa</td><td class="linia">Ogółem</td><td class="linia">do 18</td><td class="linia">powyżej 65</td><td class="linia">Kobiet</td></tr>';
	$sql3="select sl_uslugi.nazwa, count(sl_uslugi.nazwa), sl_uslugi.id from pac, wizyty, sl_uslugi where sl_uslugi.id = wizyty.typ_wiz and pac.id = wizyty.id_pac  and year(wizyty.data) = '2014' and wizyty.akt = 1  and wizyty.id_gab = ".$row[2]."  group by sl_uslugi.nazwa, sl_uslugi.id";
	#echo $sql3;
	$result3=mysql_query($sql3);
	
	while($row3 = mysql_fetch_array($result3))
	{
	$ilew[$row3[2]]=$row3[1];
        #$jakiew[$row[2]]=$row3
	#echo '<tr><td class="linia">'.$row3[0].'</td><td class="linia">'.$row3[1].'</td></tr>';
	}
	
	foreach ($ilew as $key => $value) {
	#print_r($value);
            if($pods[$key]['do18']<>'') $ile18 = $pods[$key]['do18'];
            else $ile18 = 0;
            
            if($pods[$key]['pow65']<>'') $ile65 = $pods[$key]['pow65'];
            else $ile65 = 0;
            
            if($pods[$key]['kob']<>'') $ilekob = $pods[$key]['kob'];
            else $ilekob = 0;
            
        echo '<tr><td class="linia">'.get_name ('sl_uslugi', $key).'</td><td class="linia">'.$ilew[$key].'</td><td class="linia">'.$ile18.'</td><td class="linia">'.$ile65.'</td><td class="linia">'.$ilekob.'</td></tr>';
}
	
	unset($pods, $ilew);
	
	
	}
	
 echo '</table>';
 
 ?>     
